package org.ttitfly.workflow.engine.mode;

import org.ttitfly.workflow.SessionHolder;
import org.ttitfly.workflow.engine.Process;
import org.ttitfly.workflow.engine.VirtualRunUnit;
import org.ttitfly.workflow.exception.ProcessRunException;


/**
 * 自动转向，通过提取名称为'toUnits'的变量值自动转向
 * 
 * @author 潘海春
 * @version 2009-9-10
 */
public class AutoTransition extends VirtualRunUnit {
	
	/** 默认转向单元的key */
	public static final String DEFAULT_TO_UNITS_KEY = "toUnits";
	
	/** 转向单元key */
	private String toUnitsKey = DEFAULT_TO_UNITS_KEY;

	/**
	 * 构造函数
	 * 
	 * @param parent
	 * @param name
	 */
	public AutoTransition(Process parent, String name, String toUnitsKey) {
		super(parent, name);
		if (toUnitsKey != null && !("".equals(toUnitsKey))) {
			this.toUnitsKey = toUnitsKey;
		}
	}

	@Override
	public void run() throws ProcessRunException {
		String toUnitStr = (String) SessionHolder.currentSession().getVariable(toUnitsKey);
		if (toUnitStr == null || "".equals(toUnitStr = toUnitStr.trim())) {
			parent.checkComplete();
			return;
		}
		String[] toUnits = toUnitStr.split(",");
		for (String to : toUnits) {
			parent.dispatch(to);
		}
	}
}
